<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="mini" :inline="true" class="demo-form-inline">
        <el-form-item label="广告图描述">
          <el-input v-model="advimgdesc" placeholder="请输入广告位名称" />
        </el-form-item>

        <el-form-item label="广告图位置">

          <el-select v-model="posId" placeholder="请选择广告位">
            <el-option
              v-for="item in allPosList"
              :key="item.id"
              :label="item.advposname"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="open('add')">新增</el-button>
        </div>
        <div>2</div>
      </el-row>

      <!-- 弹窗 -->
      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="40%"
        :close="handleClose"
      >

        <el-form ref="posForm" :model="posForm" :rules="rules" style="width:80%" label-position="right" label-width="100px" class="form">
          <el-form-item label="广告位" prop="advposname">
            <el-select v-model="posForm.advposname" placeholder="请选择活动区域">
              <el-option
                v-for="item in allPosList"
                :key="item.id"
                :label="item.advposname"
                :value="item.id"
              />

            </el-select>
          </el-form-item>

          <el-form-item label="广告图描述" prop="advimgdesc">
            <el-input v-model="posForm.advimgdesc" type="textarea" placeholder="请输入广告图描述" />
          </el-form-item>
          <el-form-item label="广告图链接" prop="advimglink">
            <el-input v-model="posForm.advimglink" placeholder="请输入广告图链接" />
          </el-form-item>
          <el-form-item label="上传图片" prop="advimgsrc">
            <el-upload
              label="上传图片"
              class="upload-demo"
              :header="token"
              action="http://124.223.14.236:8060/admin/common/upload?type=images"

              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
            </el-upload>

          </el-form-item>

          <el-form-item>
            <el-button type="primary">确认</el-button>
            <el-button @click="handleClose">取消</el-button>
          </el-form-item>
        </el-form>

      </el-dialog>

    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table
        :data="list"
        style="width: 100%"
        border
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column prop="index" label="#" width="70" />
        <el-table-column prop="advposname" label="广告图位" />
        <el-table-column prop="advimgdesc" label="广告图描述" />
        <el-table-column prop="advimgsrc" label="广告图">
          <template #default="{row}">
            <img style="width:150px" :src="'http://124.223.14.236:8060/' + row.advimgsrc" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="advimglink" label="广告图链接" />
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="$router.push('/content_article/add')" />
            <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域 -->
    <div class="page">
      <el-pagination
        :current-page="qaueryData.page"
        :page-sizes="[1,2,5, 10, 20, 30]"
        :page-size="qaueryData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

  </div>
</template>
<script>
import { getAllPos, getImg } from '@/api/adv/advimg'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      total: 0,
      qaueryData: {
        advposdesc: '',
        advposname: '',
        page: 1,
        limit: 10
      },
      rules: {
        advposname: [{ required: true, message: '请选择广告位', trigger: 'change' }],
        advimgdesc: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        advimgsrc: [{ required: true, message: '内容不能为空', trigger: 'change' }],
        advimglink: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      },
      posForm: {
        advposname: '',
        advimgdesc: '',
        advimgsrc: '',
        advimglink: ''

      },
      title: '', // 弹框标题
      dialogVisible: false, // 控制弹窗显示影藏
      allPosList: [],
      posId: '',
      advimgdesc: '',
      list: []
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  created() {
    this.initData()
  },
  methods: {
    open(type) {
      this.dialogVisible = true
    },
    handleClose() {
      // done()
      this.dialogVisible = false
    },
    // 更改每一页的条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.qaueryData.limit = val
      this.qaueryData.page = 1
      this.initData()
    },
    // 更改页数
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.qaueryData.page = val
      this.initData()
    },
    // 重置
    reset() {
      // this.$refs.allPosList.resetFields()
      this.initData()
    },
    // 搜索
    async search() {
      console.log(this.posId)
      console.log(this.advimgdesc)
      const res = await getImg({
        advimgdesc: this.advimgdesc,
        advimgpos: this.posId,
        page: 1,
        limit: 10
      })
      console.log(res)
      this.list = res.data.data
      // this.initData()
    },
    // 初始化渲染
    async initData() {
      const res1 = await getAllPos()
      // console.log(res)
      this.allPosList = res1.data
      const res2 = await getImg(this.qaueryData)
      // console.log(res2)
      this.total = res2.data.count
      this.list = res2.data.data
    }
  }
}
</script>
<style lang="scss" scoped>
  .page-box {
    padding: 20px;
    .search-box,.tabbar-box {
      padding-top: 10px;
      border:  1px solid #eee;
      margin-bottom: 20px;
      height: 50px;
    }
  }

</style>
